package com.weilele.base.library

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentContainerView
import androidx.viewbinding.ViewBinding
import com.weilele.mvvm.base.MvvmActivity
import com.weilele.mvvm.base.MvvmFragment
import com.weilele.mvvm.utils.activity.navigateTo
import com.weilele.mvvm.utils.fragment.addFragment
import com.weilele.mvvm.utils.result_contract.navigateForResult
import com.weilele.mvvm.utils.safeGet

/**
 * 包含fragment的activity
 */
open class FragmentContainerActivity : BaseActivity<ViewBinding?>() {
    companion object {
        private const val KEY_FRAGMENT_CLASS_NAME = "KEY_FRAGMENT_CLASS_NAME"
        private const val KEY_FRAGMENT_BUNDLE = "KEY_FRAGMENT_BUNDLE"

        /**
         * 在activity启动数据
         */
        fun <T : Fragment> start(
            appCompatActivity: AppCompatActivity,
            cls: Class<T>,
            bundle: Bundle? = null,/*此数据将会传递给fragment*/
            option: ActivityOptionsCompat? = null,
        ) {
            appCompatActivity.navigateTo<FragmentContainerActivity>(option = option) {
                putData(it, cls, bundle)
            }
        }

        /**
         * 在activity启动数据
         */
        fun <T : Fragment> startForResult(
            appCompatActivity: MvvmActivity,
            cls: Class<T>,
            bundle: Bundle? = null,
            option: ActivityOptionsCompat? = null,
            onResult: (resultCode: Int, resultData: Intent?) -> Unit
        ) {
            appCompatActivity.navigateForResult<FragmentContainerActivity>(
                option = option,
                setParams = {
                    putData(it, cls, bundle)
                },
                onResult = onResult
            )
        }

        /**
         * 在fragment启动activity
         */
        fun <T : Fragment> start(
            fragment: Fragment,
            cls: Class<T>,
            bundle: Bundle? = null,
            option: ActivityOptionsCompat? = null,
        ) {
            fragment.navigateTo<FragmentContainerActivity>(option = option) {
                putData(it, cls, bundle)
            }
        }

        fun <T : Fragment> startForResult(
            fragment: MvvmFragment,
            cls: Class<T>,
            bundle: Bundle? = null,
            option: ActivityOptionsCompat? = null,
            onResult: (resultCode: Int, resultData: Intent?) -> Unit
        ) {
            fragment.navigateForResult<FragmentContainerActivity>(
                option = option,
                setParams = {
                    putData(it, cls, bundle)
                },
                onResult = onResult
            )
        }

        /**
         * 设置携带数据
         */
        private fun <T : Fragment> putData(intent: Intent, cls: Class<T>, bundle: Bundle? = null) {
            intent.putExtra(KEY_FRAGMENT_CLASS_NAME, cls)
            bundle?.let {
                intent.putExtra(KEY_FRAGMENT_BUNDLE, bundle)
            }
        }
    }

    override fun onCreateContentView(inflater: LayoutInflater, parent: ViewGroup?): View? {
        return FragmentContainerView(this).apply {
            id = R.id.base_library_fragment_container_id
            intent.getSerializableExtra(KEY_FRAGMENT_CLASS_NAME).safeGet<Class<Fragment>>()?.let {
                addFragment(id, it.newInstance().apply {
                    arguments = intent.getBundleExtra(KEY_FRAGMENT_BUNDLE)
                })
            }
        }
    }
}